﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Model.WXshopBuiness;
using Xproject.Framework.Core.Repositories;
using Xproject.Framework.Utility.Extensions;

namespace Repository.WXshopBuiness
{
  public static   class ProductCommentsRep
    {
        public static ProductComments GetById(this IRepositoryAsync<ProductComments> repository, string ID)
        {
            Expression<Func<ProductComments, bool>> express = i => i.Enable == 1;
            if (!string.IsNullOrEmpty(ID))
            {
                express = express.AndAlso(i => i.ID.Contains(ID));
            }
            else
            {
                return null;
            }
            return repository.Query().Where(express).Select().FirstOrDefault();
        }

        public static List<ProductComments> GetCommentsList(this IRepositoryAsync<ProductComments> repository, string productName, string productCode, DateTime? startTime, DateTime? endTime, string order, string ordername)
        {
            Expression<Func<ProductComments, bool>> express = i => i.Enable == 1;
            if (!string.IsNullOrEmpty(productName))
            {
                express = express.AndAlso(i => i.ProductName.Contains(productName));
            }
            if (!string.IsNullOrEmpty(productCode))
            {
                express = express.AndAlso(i => i.ProductCode.Contains(productCode));
            }
            if (startTime.HasValue)
            {
                express = express.AndAlso(i => i.CreateTime>= startTime.Value);
            }
            if (endTime.HasValue)
            {
                express = express.AndAlso(i => i.CreateTime <= endTime.Value);
            }
            return repository.Query().Where(express).Select().OrderByDescending(i => i.ProductCode).ThenByDescending(i=>i.CommentIndex).ToList();
        }

        /// <summary>
        /// 得到产品的评论
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="productId"></param>
        /// <returns></returns>
        public static List<ProductComments> GetProductComments(this IRepositoryAsync<ProductComments> repository, string productId)
        {
            Expression<Func<ProductComments, bool>> express = i => i.Enable == 1;
            if (!string.IsNullOrEmpty(productId))
            {
                express = express.AndAlso(i => i.ProductID.Contains(productId));
            }
            else
            {
                return null;
            }
            return repository.Query().Where(express).Select().OrderByDescending(i => i.CommentIndex).ToList();
        }
        /// <summary>
        /// 得到我所评论过的商品评语
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="productId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static ProductComments GetMyComments(this IRepositoryAsync<ProductComments> repository,string orderId, string productId, string userId)
        {
            Expression<Func<ProductComments, bool>> express = i => i.Enable == 1;
            if (!string.IsNullOrEmpty(productId))
            {
                express = express.AndAlso(i => i.ProductID==productId);
            }
            else
            {
                return null;
            }
            if (!string.IsNullOrEmpty(userId))
            {
                express = express.AndAlso(i => i.UserId==userId);
            }
            else
            {
                return null;
            }
            if (!string.IsNullOrEmpty(orderId))
            {
                express = express.AndAlso(i => i.OrderId == orderId);
            }
            else
            {
                return null;
            }

            return repository.Query().Where(express).Select().FirstOrDefault();
        }
    }
}
